package com.example.goodog.activities;


import android.app.Activity;
import android.content.Intent;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

import com.example.goodog.R;
import com.example.goodog.customViews.ClickerButton;

public class ToolsActivity extends Activity {

	LinearLayout ll;
	ClickerButton clicker;
	SeekBar seekBar;
	Button PlaySoundButton;
	TextView FreqTwxtView;
	
	
	 private final int duration = 3; // seconds
	 private final int sampleRate = 44100;
	 private final int numSamples = duration * sampleRate;
	 private final double sample[] = new double[numSamples];

	 private final byte generatedSnd[] = new byte[2 * numSamples];

	 Handler handler = new Handler();
	

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_tools);

		
		ll = (LinearLayout) findViewById(R.id.mainLL);
		clicker = new ClickerButton(this);
		seekBar = new SeekBar(this);
		PlaySoundButton = new Button(this);
		FreqTwxtView = new TextView(this);    
		
		seekBar.setMax(23000);
		seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			
			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				FreqTwxtView.setText(String.valueOf(progress));
				
			}
		});
		PlaySoundButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// Use a new tread as this can take a while
				  Thread thread = new Thread(new Runnable() {
				      public void run() {
				        genTone();
				        handler.post(new Runnable() {
				    
				    public void run() {
				     playSound();
				    }
				   });
				      }   
				    });
				    thread.start();
			}
		});
		
		
		ll.addView(clicker);
		ll.addView(seekBar);
		ll.addView(FreqTwxtView);
		
		ll.addView(PlaySoundButton);
		
		
		
	}
	
	void genTone(){
		// fill out the array
		for (int i = 0; i < numSamples; ++i) {
			int Freq = seekBar.getProgress();
			if (Freq == 0)
			{
				Freq =1;
			}
			sample[i] = Math.sin(2 * Math.PI * i / (sampleRate/Freq));
		}

		// convert to 16 bit pcm sound array
		// assumes the sample buffer is normalised.
		int idx = 0;
		for (double dVal : sample) {
			short val = (short) (dVal * 32767);
			generatedSnd[idx++] = (byte) (val & 0x00ff);
			generatedSnd[idx++] = (byte) ((val & 0xff00) >>> 8);
		}
	}

	void playSound(){
		AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
				sampleRate, AudioFormat.CHANNEL_CONFIGURATION_MONO,
				AudioFormat.ENCODING_PCM_16BIT, numSamples,
				AudioTrack.MODE_STATIC);
		audioTrack.write(generatedSnd, 0, numSamples);
		audioTrack.play();
	}
		
	
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		super.onActivityResult(requestCode, resultCode, data);

	} 

}
